<?php
/**
 * @desc 水汽联运管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_smotor extends ctrl_base {
    private static $dirName = 'smotor';
    
    /**
     * @desc 地区列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('smotor_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$province = isset($_POST['province']) ? trim($_POST['province']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$area_wharf = isset($_POST['area_wharf']) ? trim($_POST['area_wharf']) : '';
		//$port_dest = isset($_POST['port_dest']) ? trim($_POST['port_dest']) : '';
    	//搜索条件组合
		$condition = array();
    	if(!empty($province)) {
    		$condition['province'] = $province;
    	}
		if(!empty($city)) {
    		$condition['city'] = $city;
    	}
		if(!empty($area_wharf)) {
    		$condition['area_wharf'] = $area_wharf;
    	}
		/*if(!empty($port_dest)) {
    		$condition['port_dest'] = $port_dest;
    	}*/
		//订单获取
    	$area_list = model_smotor :: get_smotor_list($nextpage,$pagesize,$condition);
    	$total = array_pop($area_list);
		//模板赋值
    	self::smarty() -> assign('area_list', $area_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('city', $city);
		self::smarty() -> assign('area_wharf', $area_wharf);
		//self::smarty() -> assign('port_dest', $port_ship);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/smotor_list.html');    	
    }
	//添加地区
	public static function smotor_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('smotor_add'));
		if($_POST){

			$smotor = self::get_request_data();
			$smotor['editer'] = $_SESSION['admin']['username'];
			$smotor['author'] = $_SESSION['admin']['username'];
			if(model_smotor::smotor_exists($smotor['area_id'], $smotor['area_wharf'])){
				self::ajax_ouput(300, '添加失败,该地址已存在');
			}
			$result = model_smotor::smotor_add($smotor);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','smotor','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		$province = model_area::get_province_list();
		$city = array(array("area_id"=>"","city"=>"请选择城市"));
		array_unshift($province, array("province"=>"请选择省份"));
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('city', $city);
		self::smarty() -> display(self::$dirName . '/smotor_add.html');
	}
	//删除订单
	public static function smotor_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('smotor_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$transport = model_smotor::get_smotor_by_id($v);
			$tmp = model_transporttmp::get_tmp_by_transport(4, $v);
			if(!empty($tmp)){
				self::ajax_ouput(300, "操作失败,水汽联运模板有 {$transport['province']}/{$transport['city']}/{$transport['area_wharf']} 的地区关联");
			}
			$id[] = $v;
		}
    	$data = array('delete_flag' => 1);

    	$st = model_smotor::update_smotor_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','smotor');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑区域
	 public static function smotor_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$smotor = model_smotor::get_smotor_by_id($id);
		$ctrl_tag = 'smotor_edit_other';
		if($smotor['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'smotor_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$smotor = array();

			$smotor = self::get_request_data();
			$smotor['editer'] = $_SESSION['admin']['username'];
			if(model_smotor::smotor_exists($smotor['area_id'], $smotor['area_wharf'], $smotor['id'])){
				self::ajax_ouput(300, '修改失败,该地址已存在');
			}
			unset($smotor['id']);

			$result = model_smotor::update_smotor_info(explode(',',$id), $smotor);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','smotor','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
    	self::smarty() -> assign('id', $id);
    	self::smarty() -> assign('smotor', $smotor);
		$province = model_area::get_province_list();
		$city = model_area::get_city_list($smotor['province']);
		array_unshift($province, array("province"=>"请选择省份"));
		array_unshift($city, array("area_id"=>"","city"=>"请选择城市"));
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('city', $city);
    	self::smarty() -> display(self::$dirName . '/smotor_edit.html');
    }
	//批量上传模板下载
	public static function get_betch_model(){
		self::download_xls_output(PATH_WWW.DOWNLOAD_FILES_PATH.'betch_area_smotor'.self::$_excel_pre);
	}
	//批量上传
	public static function batch(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('smotor_add'));
		//文件上传
		$info = self::file_upload_xls();
		//xls文件解析
		framework_base_autoloader::import_vendor('excel/PHPExcel.php');
		$objPHPExcel = new PHPExcel();
		$objPHPExcel = PHPExcel_IOFactory::load($info['url']);
		$csivit = $objPHPExcel->getSheet(0)->toArray();
		//数据读取
		unset($csivit[0]);
		foreach($csivit as $key => $value){
			$len = count($value);
			$csivit[$key][$len] = $_SESSION['admin']['username'];
			$csivit[$key][$len + 1] = $_SESSION['admin']['username'];
		}
		$result = model_smotor::batch_insert_smotor($csivit);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','smotor','closeCurrent');
		}else{
			self::ajax_ouput(300, '操作失败,请重新上传');
		}
	}
}
